<?php
	include_once("db/PayStation.inc");
	include_once("db/Ministry.inc");
	include_once("db/Department.inc");
	include_once("db/Province.inc");
	include_once("db/Region.inc");
	include_once("db/District.inc");
	
	$ministryId = null;
	$departmentId = null;
	$id = null;
	
	if (isset($_REQUEST["ministry"]))	
		$ministryId = $_REQUEST["ministry"];
		
	if (isset($_REQUEST["department"]))
		$departmentId = $_REQUEST["department"];
		
	if (isset($_REQUEST["id"]))
		$id = $_REQUEST["id"];
	
	$ps = null;
	$isNewPayStation = false;
	
	if ($ministryId != null && $departmentId != null && $id != null) {
		$ps = PayStation::getPayStation($ministryId, $departmentId, $id);
	} else {
		$ps = new PayStation();
		$isNewPayStation = true;
	}
	
	$ministry = null;
	$department = null;
	$region = null;
	 
	if ($ministryId != null) 
		$ministry = Ministry::getMinistry($ministryId)->description;
		
	if ($departmentId != null)
		$department = Department::getDepartment($departmentId)->description;
	
	if (!$isNewPayStation)
		$region = Region::getRegion($ps->regionCode)->name;
?>

<script language="javascript"><!--
	function changeProvince() {
		
		var newProvinceId = document.getElementById("province").value;
		var ministryId = document.getElementById("ministryId").value;
		var deptId = document.getElementById("deptCode").value;
		var regionId = document.getElementById("region").value;
		var description = document.getElementById("description").value;
		var code = document.getElementById("id").value;
		
		var url = 
			"?action=newPayStation" + 
			"&province=" + newProvinceId + 
			"&ministry=" + ministryId +
			"&department=" + deptId +
			"&region=" + regionId +
			"&description=" + escape(description) + 
			"&code=" + code;

		window.location.href = url;
	}
	
--></script>

<h1>Add New Pay Station</h1>

<form method="post" action="?action=addPayStation">
	<table cellspacing="0" class="formLayout">
		<?php if (isset($_REQUEST["error"]) && $_REQUEST["error"] == "payCodeExists") { ?>
			<tr>
				<td colspan="2" class="error">
					The pay station code you have entered already exists.  Please
					use another one.
				</td>
			</tr>
		<?php } ?>
		<tr>
			<td>Code</td>
			<td><input type="text" id="id" name="id" maxlength="4" value="<?php 
				if (isset($_REQUEST["code"]))
					print $_REQUEST["code"];
			?>" /></td>
		</tr>
		<tr>
			<td>Ministry</td>
			<td><select id="ministryId" name="ministryId">";
				<?php
					$ministry = Ministry::getAllMinistries();
	
					foreach ($ministry as $m) {
						print "<option value=\"$m->id\"";
						
						if (isset($_REQUEST["ministry"]) && $_REQUEST["ministry"] == $m->id)
							print "selected=\"selected\"";	
								
						print ">$m->id - $m->description</option>";
					}
				?>	
				</select></td>
		</tr>
		<tr>
			<td>Department</td>
			<td><select id="deptCode" name="deptCode">";
				<?php
					$dept = Department::getAllDepartments();
	
					foreach ($dept as $D) {
						print "<option value=\"$D->id\"";
						
						if (isset($_REQUEST["department"]) && $_REQUEST["department"] == $D->id)
							print "selected=\"selected\"";
							
						print ">$D->id - $D->description</option>";
					}
				?>	
				</select></td>
		</tr>
		<tr>
			<td>Region</td>
			<td><select id="region" name="region">";
				<?php
					$region = Region::getAllRegions();
	
					foreach ($region as $r) {
						print "<option value=\"$r->id\"";
							
							if (isset($_REQUEST["region"]) && $_REQUEST["region"] == $r->id)
								print "selected=\"selected\"";
						
						print ">$r->id - $r->name</option>";
					}
				?>	
				</select></td>
		</tr>
		<tr>
			<td>Description</td>
			<td><input type="text" id="description" name="description" size="40" maxlength="40" value="<?php 
				if (isset($_REQUEST["description"]))
					print $_REQUEST["description"];
			?>" /></td>
		</tr>
		<tr>
			<td>Province</td>
			<td>
				<select onChange="changeProvince()" id="province" name="province">";
					<option value="0"></option>
				<?php
					$prov = Province::getAllProvinces();
	
					foreach ($prov as $p) {
						print "<option value=\"$p->id\"";
						if ($_REQUEST["province"] == $p->id) print " selected=\"selected\"";		
						print ">$p->id - $p->name</option>";
					}
				?>	
				</select>
			</td>
		</tr>
		<tr>
			<td>District</td>
			<td><?php showDistrictDropDown($_REQUEST["province"], '') ?></td>
		</tr>
		<tr colspan="2">
			<td>
				<input type="submit" value="Confirm" class="button"/>
				<input type="button" value="Cancel" class="button" onClick="window.location.href='.?action=viewPayStations'" />
			</td>
		</tr>
	</table>
</form>